﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using Texfina.Core.Common;

namespace GenCnx
{
   public partial class frmGenCnx : Form
   {
      XmlDocument xmlDoc = new XmlDocument();

      public frmGenCnx()
      {
         InitializeComponent();
      }

      private void bttCancel_Click(object sender, System.EventArgs e)
      {
         this.Close();
      }

      private void bttFile_Click(object sender, System.EventArgs e)
      {
         OpenFileDialog ofd = new OpenFileDialog();
         ofd.Filter = "XML Files (*.xml)|*.xml";
         if (ofd.ShowDialog() != DialogResult.OK)
         {
            ofd.Dispose();
            return;
         }

         txtFile.Text = ofd.FileName;
         ofd.Dispose();
      }


      private void txtFile_TextChanged(object sender, System.EventArgs e)
      {
         LoadConfigDataXml();
      }

      private void bttOk_Click(object sender, System.EventArgs e)
      {
         CreateConfigDataXml();
      }

      private void CreateConfigDataXml()
      {
         //XmlDocument docXML = new XmlDocument();
         //XmlElement elmRoot = docXML.CreateElement("ConfigData");

          try
          {
           Cryptor objCryptor = new Cryptor();

         //XmlElement elmCnx = docXML.CreateElement(cbnNodo.SelectedItem.ToString());

          XmlNode nodCnx = xmlDoc.SelectSingleNode("/ConfigData/" + cbnNodo.SelectedItem);

          nodCnx.Attributes["Server"].Value = Convert.ToBase64String(objCryptor.Encrypt(txtSrv.Text));
          nodCnx.Attributes["Database"].Value = Convert.ToBase64String(objCryptor.Encrypt(txtDB.Text));
          nodCnx.Attributes["User"].Value = Convert.ToBase64String(objCryptor.Encrypt(txtUsr.Text));
          nodCnx.Attributes["Password"].Value = Convert.ToBase64String(objCryptor.Encrypt(txtPsw.Text));


         //XmlAttribute xmlAt = docXML.CreateAttribute("Server");
         //xmlAt.Value = Convert.ToBase64String(objCryptor.Encrypt(txtSrv.Text));
         //elmCnx.Attributes.Append(xmlAt);

         //xmlAt = docXML.CreateAttribute("Database");
         //xmlAt.Value = Convert.ToBase64String(objCryptor.Encrypt(txtDB.Text));
         //elmCnx.Attributes.Append(xmlAt);

         //xmlAt = docXML.CreateAttribute("User");
         //xmlAt.Value = Convert.ToBase64String(objCryptor.Encrypt(txtUsr.Text));
         //elmCnx.Attributes.Append(xmlAt);

         //xmlAt = docXML.CreateAttribute("Password");
         //xmlAt.Value = Convert.ToBase64String(objCryptor.Encrypt(txtPsw.Text));
         //elmCnx.Attributes.Append(xmlAt);

         //elmRoot.AppendChild(elmCnx);

         //docXML.AppendChild(elmRoot);

         //docXML.Save(txtFile.Text);

          xmlDoc.Save(txtFile.Text);


         MessageBox.Show("Nodo [" + cbnNodo.SelectedItem + "] grabado Correctamente.");
          }
          catch(Exception e)
          {
              MessageBox.Show(e.Message);
          }

         
         //this.Close();
      }


      private void LoadConfigDataXml()
      {
         if (string.IsNullOrEmpty(txtFile.Text))
         {
            grpConfigData.Enabled = false;
            return;
         }
                   
         xmlDoc.Load(txtFile.Text);
          
         XmlNodeList ListnodCnx = xmlDoc.SelectSingleNode("/ConfigData").ChildNodes;

         if (ListnodCnx == null)
            return;

         foreach (XmlNode nodXml in ListnodCnx)
         {
             cbnNodo.Items.Insert(0, nodXml.Name);
         }
          
         grpConfigData.Enabled = true;
          
      }

      private void cbnNodo_SelectedIndexChanged(object sender, EventArgs e)
      {
          XmlNode nodCnx = xmlDoc.SelectSingleNode("/ConfigData/" + cbnNodo.SelectedItem);

          if (nodCnx == null)
              return;

          Cryptor objCryptor = new Cryptor();

          txtSrv.Text = Encoding.UTF8.GetString(objCryptor.Decrypt(Convert.FromBase64String(nodCnx.Attributes["Server"].Value)));
          txtDB.Text = Encoding.UTF8.GetString(objCryptor.Decrypt(Convert.FromBase64String(nodCnx.Attributes["Database"].Value)));
          txtUsr.Text = Encoding.UTF8.GetString(objCryptor.Decrypt(Convert.FromBase64String(nodCnx.Attributes["User"].Value)));
          txtPsw.Text = Encoding.UTF8.GetString(objCryptor.Decrypt(Convert.FromBase64String(nodCnx.Attributes["Password"].Value)));

          bttOk.Enabled = true;
      }
   }
}
